<?php
/**
 * 挂件开发：清风  QQ:9516097
 * @license  http://www.hnqss.cn/tools/ QingFeng License
 */
function theme_widget_qf_samecat(&$setting,&$env){
	$_return = array();	
	$master = $setting['master']?$setting['master']:0;//是否取父类下的所有子类 true 是，false 否 只取同级分类
	//print_r($master);exit;
	
	//获得对应页面ctl
	$pagectl = app::get('site')->router()->get_query_info('module');	
	//取得不同页面的cat_id和parent_id
	
	switch ($pagectl) {
        case'gallery':
			$o=$env->pagedata['screen'];
			$cat_id = $o['cat_id'];//当前商品分类id
			$parent_id = _getParentid($cat_id);//获取父分类id
			$search_cat = $o['search_cat'];//搜索相关分类      
			//print_r($parent_id);exit;
		break;
        case 'product':
			$o=$env->pagedata['page_product_basic'];
			$goods_id = $o['goods_id'];//商品id
			$cat_id  = _get_cat_id($goods_id);;//根据商品id获取商品分类
			$parent_id = _getParentid($cat_id);//父分类id
        break;
    }//end switch
	
	//print_r($o2);exit;
    if ($cat_id){//搜索时，$cat_id为空,所以需要判断一下，搜索为空时，取search_cat页面数据
		$_return['curr1'] = $cat_id;
		$_return['curr2'] = $parent_id;
		
		$getCat = b2c_widgets::load('GoodsCat');
		if ($master == 1 && $parent_id){
			//如果选择的是不递归父类，取同级分类
			$_return['cat'] = $getCat->getGoodsCatMap($parent_id,false);
		}else{
			//递归父类，取父类下的所有分类（三级）
			$_return['cat'] = $getCat->getGoodsCatMap($cat_id,true);
		}
    }
	elseif ($search_cat){//搜索结果页面自动取出关联的商品分类，系统自动给取出来了	
		/*下面的处理只是为了让所有页面显示效果一致*/
		$_return['cat'][0]['catId']='0';
		$_return['cat'][0]['catName']='所有分类';				
		$_return['cat'][0]['catLink']= '';
		foreach($search_cat as $k =>$v){
			$_return['cat'][0]['items'][$k]['catId']= $v['cat_id'];
			$_return['cat'][0]['items'][$k]['catName']= $v['cat_name'];
			$_return['cat'][0]['items'][$k]['catLink']= _getUrl($v['cat_id']);
		}
	}
    return $_return;
}
//根据商品id取分类id
function _get_cat_id($goodsId){
	$objgoodscat = app::get('b2c')->model('goods');
	$catinfo = $objgoodscat->getList('goods_id,cat_id,type_id',array('goods_id'=>$goodsId));
	return $catinfo[0]['cat_id'];
}
//根据分类id取父类id
function _getParentid($catid){
	$objprentcat = app::get('b2c')->model('goods_cat');
	$getParent = $objprentcat->getList('cat_id,parent_id,cat_path,cat_name',array('cat_id'=>$catid));
	return $getParent[0]['parent_id'];
}
//生成分类链接
function _getUrl($catId){
	return $o = app::get('site')->router()->gen_url(array('app'=>'b2c', 'ctl'=>'site_gallery','args'=>array($catId)));
}
?>